<template>
	<view class="content">
		<view class="title">
			<text class="iconfont icon-shop"></text>
			<text>优购生活馆</text>
		</view>
		<!-- 商品列表 -->
		<view class="goods-list">
			<view class="goods-item" v-for="(item, index) in goodsList" :key="index" @click="toggleCheck(item)">
				<text class="iconfont" :class="item.checked?'icon-checked':'icon-uncheck'"></text>
				<image :src="item.goods_small_logo" alt="" />
				<view class="right">
					<text class="text-line2">{{item.goods_name}}</text>
					<view class="btm">
						<text class="price">￥<text>{{item.goods_price}}</text>.00</text>
						<view class="goods-num">
							<button :disabled="item.num<2" @click.stop="sub(item)">-</button>
							<text>{{item.num}}</text>
							<button @click.stop="add(item)">+</button>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="account">
			<view class="select-all">
				<text class="iconfont" :class="isAll?'icon-checked':'icon-uncheck'" @click="toggleAll"></text>
				<text>全选</text>
				<view class="price-wrapper">
					<view class="price">
						<text>合计:<text class="num">￥{{totalPrice}}.00</text></text>
						<view class="info">包含运费</view>
					</view>
					<view class="account-btn" @click="toPay">结算({{totalNum}})</view>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				goodsList: []
			}
		},
		// 商详添加商品,跳转到购物车页面时候,需要更新界面
		onShow() {
			// 取
			this.cart = this.$store.getters.getCart
			// 设置购物车右上角的文本
			uni.setTabBarBadge({
				index: 2,
				text: (this.cart).length + ''
			})
			
			// 如果购物车数据是空的,没必要发请求
			if (!this.cart.length) {
				this.goodsList = [] //如果购物车为空，那么商品列表也为空
				return
			}
			// 取出cart的每一项的goodsId拼接,以逗号分隔
			// 方案1
			// let str =''
			// cart.forEach(item=>{
			// 	str+=item.goodsId+','
			// })
			//去掉最后的一个逗号
			//方案2，map，join
			let idsArr = this.cart.map(item => {
				return item.goodsId
			})

			let idsStr = idsArr.join(',')
			console.log(idsStr)
			this.queryGoodsList(idsStr)
		},
		computed: {
			totalNum() {
				// 遍历商品列表，把所有选中元素的数量相加
				// let sum = 0
				// this.goodsList.forEach(item => {
				// 	sum += item.checked ? item.num : 0
				// })
				// return sum
				return this.goodsList.reduce((sum, item) => {
					return sum + (item.checked ? item.num : 0)
				}, 0)
			},
			// 总价
			totalPrice() {
				return this.goodsList.reduce((sum, item) => {
					return sum + (item.checked ? item.num * item.goods_price : 0)
				}, 0)
			},
			isAll: {
				get() {
					// 方案1：所在勾选商品的数量===所有商品，全选勾选
					// let sum = 0
					// this.goodsList.forEach(item => {
					// 	if (item.checked) {
					// 		sum++
					// 	}
					// })
					// return sum === this.goodsList.length
					// 方案2:如果有一个不勾选的话，那全选就不勾选
					// this.goodsList.forEach(item => {
					// 	// 找到一个不勾选的
					// 	if (!item.checked) {
					// 		return false
					// 	}
					// })
					// return true
					// for (let item of this.goodsList) {
					// 	// 找到一个不勾选的,全选不勾选 
					// 	if (!item.checked) {
					// 		return false
					// 	}
					// }
					// return true
					// 如果所有商品勾选的话，全选勾选
					// every遍历数组,给数组每一项执行指定函数,如果所有的函数返回值都是true,最终会返回true
					return this.goodsList.every(item => {
						return item.checked
					})
				},
				set(status) {
					// console.log(status)
					this.goodsList.forEach(item => {
						item.checked = status
					})
				}
			}
		},
		watch: {
			// 商品数量与状态的改变本质上改变goodsList
			goodsList: {
				handler() {
					// console.log(newValue)
					this.$store.commit('updateCart',this.goodsList)
				},
				deep: true
			}
		},
		methods: {
			// 跳转支付页面
			toPay() {
				// 如果商品数量为0,提示
				if (!this.totalNum) {
					uni.showToast({
						title: '请选择要购买的商品',
						icon: 'none'
					})
					return
				}
				// 如果没有登录,跳转登录
				let token = uni.getStorageSync('token')
				if (!token) {
					uni.navigateTo({
						url: '/pages/login/login'
					})
					// 跳转后,后面的逻辑依然会执行,一定需要return
					return
				}

				uni.navigateTo({
					url: '/pages/pay/pay'
				})
			},
			// 全选勾选与/不勾选
			toggleAll() {
				this.isAll = !this.isAll
			},
			// +按钮,数量++
			add(item) {
				item.num++
			},
			// -按钮,数量--
			sub(item) {
				item.num--
			},
			// 点击每一个商品里,取反它的选中状态
			toggleCheck(item) {
				item.checked = !item.checked
			},
			async queryGoodsList(idsStr) {
				let goodsList = await this.$request({
					url: '/api/public/v1/goods/goodslist?goods_ids=' + idsStr
				})

				// 遍历cart，取出每一项goodsId，然后，找到后，
				this.goodsList = this.cart.map(item => {
					// 在goodsList找一项商品Id和goodsId相等的那一项
					let targetGoods = goodsList.find(goods => {
						return goods.goods_id === item.goodsId
					})

					//把那一项和cart元素合并
					return { ...item,
						...targetGoods
					}
				})
			}
		}
	}
</script>

<style lang="less">
	.iconfont {
		font-size: 44rpx;
	}

	.title {
		height: 88rpx;
		display: flex;
		align-items: center;

		.iconfont {
			color: #999;
			margin: 6rpx 12rpx 0 32rpx;
		}
	}

	.goods-item {
		height: 206rpx;
		background-color: #fff;
		border-top: 1rpx solid #ddd;
		display: flex;
		box-sizing: border-box;
		align-items: center;

		.iconfont {
			color: #eb4450;
			margin: 0 30rpx;
		}

		image {
			width: 160rpx;
			height: 160rpx;
		}

		.right {
			flex: 1;
			display: flex;
			flex-direction: column;
			margin: 0 20rpx 0 18rpx;

			.btm {
				display: flex;
				margin-top: 40rpx;
				justify-content: space-between;

				.price {
					color: #eb4450;
					display: flex;

					>text {
						font-size: 24px;
					}
				}

				.goods-num {
					display: flex;
					align-items: center;

					button {
						width: 60rpx;
						height: 50rpx;
						box-sizing: border-box;
						display: flex;
						justify-content: center;
						align-items: center;
						border: 4rpx solid #666;
					}

					text {
						margin: 0 30rpx;
					}
				}
			}
		}
	}

	.goods-list {
		position: absolute;
		bottom: 98rpx;
		top: 88rpx;
		width: 100%;
		overflow: auto;
		padding-bottom: 60rpx;
		background-color: #f4f4f4;
	}

	.account {
		height: 98rpx;
		border-bottom: 1rpx solid #ddd;
		display: flex;
		position: absolute;
		left: 0;
		right: 0;
		bottom: 0;
		align-items: center;

		.iconfont {
			color: #eb4450;
			margin: 0 30rpx;
		}

		.account-btn {
			width: 230rpx;
			height: 98rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #fff;
			background-color: #eb4450;
		}

		.select-all {
			flex: 1;
			display: flex;
			align-items: center;

			.price-wrapper {
				display: flex;
				flex: 1;
				justify-content: flex-end;
				align-items: center;
			}
		}

		.price {
			margin-right: 20rpx;

			.num {
				color: #eb4450;
			}

			.info {
				color: #999;
			}
		}
	}
</style>
